package com.dropdb.demo.controller.admin;

import com.dropdb.demo.bean.AdminGoodsPOJO;
import com.dropdb.demo.bean.Goods;
import com.dropdb.demo.bean.bo.AdminListGoodsBO;
import com.dropdb.demo.bean.enumeration.ErrorReason;
import com.dropdb.demo.bean.vo.AdminCateAndBrandVO;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.bean.vo.BaseData;
import com.dropdb.demo.service.GoodsService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {
    @Autowired
    GoodsService goodsService;

    /**
     * 分页获取所有的商品信息
     * @param listGoodsBO 接受前端参数
     * @return json
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @RequiresPermissions(value = {"*", "admin:goods:list"}, logical = Logical.OR)
    public BaseRespVO listGoods(@Validated AdminListGoodsBO listGoodsBO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespVO.error(402, bindingResult.getFieldError().getDefaultMessage());
        }
        BaseData baseData = goodsService.ListGoods(listGoodsBO);
        return BaseRespVO.ok(baseData);
    }

    /**
     * 为上架商品提供品牌信息
     * @return json
     */
    @RequestMapping(value = "catAndBrand", method = RequestMethod.GET)
    public BaseRespVO ListCateAndBrand() {
        AdminCateAndBrandVO cateAndBrandList = goodsService.ListCateAndBrand();
        return BaseRespVO.ok(cateAndBrandList);
    }

    /**
     * 上架商品
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @RequiresPermissions(value = {"*", "admin:goods:create"}, logical = Logical.OR)
    public BaseRespVO addGoods(@Valid @RequestBody AdminGoodsPOJO addGoodsBO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespVO.error(402, bindingResult.getFieldError().getDefaultMessage());
        }
        ErrorReason result = goodsService.addGoods(addGoodsBO);
        if (result == null || result.equals(ErrorReason.INTERNAL_ERROR)) {
            return BaseRespVO.error(ErrorReason.INTERNAL_ERROR.getCode(), ErrorReason.INTERNAL_ERROR.getReason());
        }
        if (!result.equals(ErrorReason.SUCESS)) {
            return BaseRespVO.error(result.getCode(), result.getReason());
        }
        return BaseRespVO.ok();
    }


    /**
     * 获取商品细节
     * @param id
     * @return
     */
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    @RequiresPermissions(value = {"*", "admin:goods:read"}, logical = Logical.OR)
    public BaseRespVO getGoodsDetail(Integer id) {
        AdminGoodsPOJO goodsVO = goodsService.getGoodsDetailById(id);
        if (goodsVO == null) return BaseRespVO.error(403, "该商品已删除");
        return BaseRespVO.ok(goodsVO);
    }

    /**
     * 更新商品
     * @param updateGoodsBO
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @RequiresPermissions(value = {"*", "admin:goods:update"}, logical = Logical.OR)
    public BaseRespVO updateGoods(@Valid @RequestBody AdminGoodsPOJO updateGoodsBO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespVO.error(402, bindingResult.getFieldError().getDefaultMessage());
        }
        ErrorReason result = goodsService.updateGoods(updateGoodsBO);
        if (result == null) {
            return BaseRespVO.error(ErrorReason.INTERNAL_ERROR.getCode(), ErrorReason.INTERNAL_ERROR.getReason());
        }
        if (!result.equals(ErrorReason.SUCESS)) {
            return BaseRespVO.error(result.getCode(), result.getReason());
        }
        return BaseRespVO.ok();
    }

    /**
     * 删除商品
     * @param goods
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @RequiresPermissions(value = {"*", "admin:goods:delete"}, logical = Logical.OR)
    public BaseRespVO deleteGoods(@RequestBody Goods goods) {
        goodsService.deleteGoods(goods);
        return BaseRespVO.ok();
    }

}
